package com.lanhao.aot.moji;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.Map;

import com.beust.jcommander.internal.Maps;

import cn.hutool.crypto.digest.MD5;
import cn.hutool.http.HttpRequest;

public class WeatherUtil {
	final static String url = "http://content.xfyun.cn/weather/query";
	final static String appId = "d88f3698";
	final static String appKey = "ead9153d37846ff65641802f306cff15";

	public static void main(String[] args) {
		Long timestamp = System.currentTimeMillis();
		String s = appId + appKey + timestamp;
		String token = MD5.create().digestHex(s);
		String token1 = getMD5(s);
		Map<String, Object> formMap = Maps.newHashMap();
		formMap.put("appId", appId);
		formMap.put("token", token);
		formMap.put("timestamp", timestamp);
		formMap.put("city", "南昌");
//		formMap.put("startDate", "2024-06-25");
//		formMap.put("endDate", "2024-06-25");
		String respBody = HttpRequest.get(url).form(formMap).execute().body();
		System.out.println(respBody);
	}
	
	public static String getMD5(String str) {
        try {
            // 生成一个MD5加密计算摘要
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 计算md5函数
            md.update(str.getBytes());
            // digest()最后确定返回md5 hash值，返回值为8为字符串。因为md5 hash值是16位的hex值，实际上就是8位的字符
            // BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash值
            String md5 = new BigInteger(1, md.digest()).toString(16);
            //BigInteger会把0省略掉，需补全至32位
            return fillMD5(md5);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
	
	public static String fillMD5(String md5) {
        return md5.length() == 32 ? md5 : fillMD5("0" + md5);
    }
}
